package com.ganzi.manager.activiti.service.imp;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ganzi.manager.activiti.model.FormInformation;
import com.ganzi.manager.activiti.service.BusinessformationService;
import com.ganzi.manager.activiti.service.FormService;
import com.ganzi.manager.activiti.service.FormformationService;
import org.activiti.engine.RepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FormServiceImp implements FormService{

    @Resource
    private FormformationService formformationService;

    /**删除表单*/
    @Override
    public Map<String, Object> delForm(FormInformation formInformation) {
        Map<String, Object> result = new HashMap<>();
        if(formInformation != null){
            Integer id = formInformation.getId();
            formformationService.deleteById(id);
            result.put("code", "SUCCESS");
        }else{
            result.put("code", "FAILURE");
            result.put("msg", "表单信息不存在！");
        }
        return result;
    }

    /**添加表单*/
    @Override
    public Map<String, Object> addForm(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String formKey = request.getParameter("formKey");
        String name = request.getParameter("name");
        //还需要处理前台传递过来的流程参数
        Map<String,Object> map = new HashMap<>();
        map.put("form_key",formKey);
        List<FormInformation> form = formformationService.selectByMap(map);
        if(form != null && form.size()>0){
            result.put("code", "FAILURE");
            result.put("msg", "表单Key已经存在！");
        }else {
            String parseForm = request.getParameter("parseForm");
            int template = parseForm.indexOf("\"template\":");
            int parse = parseForm.indexOf("\"parse\":");
            int data = parseForm.indexOf("\"data\":");
            System.out.print("----:" + parseForm + "\n");
            String temp = parseForm.substring(template + 12, parse - 6);
            temp = replaceTemplate(temp);
            String paramData = parseForm.substring(parse + 8, data - 5);
            paramData = replaceTemplate(paramData);
            FormInformation f = FormInformation.builder().name(name).parseForm(temp).formKey(formKey).build();
            System.out.print("temp----:" + temp+ "\n");
            System.out.print("paramData-----:" + paramData+ "\n");
            //表单保存到数据库
            boolean m = formformationService.insert(f);
            if (m) {
                result.put("code", "SUCCESS");
            } else {
                result.put("code", "FAILURE");
                result.put("msg", "表单信息不存在！");
            }
        }
        return result;
    }

    /**表单list*/
    @Override
    public Map<String, Object> listForm() {
        Map<String, Object> result = new HashMap<>();
        List<FormInformation> list = formformationService.findAllList();
        result.put("code", "SUCCESS");
        result.put("data", list);
        return result;
    }

    /**查询单个form*/
    @Override
    public Map<String, Object> findOneForm(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String id = request.getParameter("id");
        FormInformation formInformation = formformationService.selectById(id);
        result.put("code", "SUCCESS");
        result.put("data", formInformation);
        return result;
    }

    /**修改表单*/
    @Override
    public Map<String, Object> updateForm(HttpServletRequest request) {
        return null;
    }

    public String replaceTemplate(String str){
        String strs = str.replace("\\", "").replace("{", "").replace("}", "")
                .replace("|", "").replace("-", "");
        return strs;
    }
}
